package cool.goodbai.controller;

import cool.goodbai.api.BaseController;
import cool.goodbai.constant.ApiConstant;
import cool.goodbai.entity.Post;
import cool.goodbai.mapper.PostMapper;
import cool.goodbai.service.impl.PostServiceImpl;
import cool.goodbai.vo.PageLazyResult;
import cool.goodbai.vo.PageResult;
import cool.goodbai.vo.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "Post")
@CrossOrigin
@RestController()
@RequestMapping(ApiConstant.BASE_API_URL + "/post/")
public class PostController extends BaseController {
    @Autowired
    private PostServiceImpl postService;

    @Autowired
    private PostMapper postMapper;

    @ApiOperation(value = "默认Get请求", notes = "请求不存在的路径时调用此默认接口")
    @ApiImplicitParam(name = "name", value = "默认字段", required = true, dataTypeClass = String.class)
    @GetMapping(value = "/{name}")
    @ResponseBody
    public String helloWorld(@PathVariable(name = "name") String name) {
        return "Hello " + name;
    }

    @ApiOperation(value = "添加岗位", notes = "添加岗位")
    @PostMapping(value = "/insert")
    @ResponseBody
    public Response<Post> insert(@NotNull @RequestBody Post post) {
        try {
            Post result = postService.insert(post);
            return getSuccessResult(result);
        } catch (RuntimeException e) {
            return getFailResult(404, "添加岗位失败");
        }
    }

    @ApiOperation(value = "删除岗位", notes = "删除岗位")
    @PostMapping(value = "/delete")
    @ResponseBody
    public Response<Post> delete(@RequestParam Long id) {
        try {
            Post result = postService.delete(id);
            return getSuccessResult(result);
        } catch (RuntimeException e) {
            return getFailResult(404, "删除岗位失败");
        }
    }

    @ApiOperation(value = "更新岗位", notes = "更新岗位")
    @PostMapping(value = "/update")
    @ResponseBody
    public Response<Post> update(@RequestBody Post post) {
        try {
            Post result = postService.update(post);
            return getSuccessResult(result);
        } catch (RuntimeException e) {
            return getFailResult(404, "更新岗位失败");
        }
    }

    @ApiOperation(value = "查询岗位", notes = "查询岗位")
    @PostMapping(value = "/select")
    @ResponseBody
    public PageResult<List<Post>> select(@RequestParam Integer current, @RequestParam Integer pageSize,
                                         @RequestParam(required = false) String name, @RequestParam(required = false) String college,
                                         @RequestParam(required = false) String description, @RequestParam(required = false) String require,
                                         @RequestParam(required = false) String education, @RequestParam(required = false) String degree,
                                         @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime deadLine,
                                         @RequestParam(required = false) Boolean valid, @RequestParam(required = false) Long id) {
        try {
            Post post = new Post();
            post.setName(name)
                    .setCollege(college)
                    .setDescription(description)
                    .setRequire(require)
                    .setEducation(education)
                    .setDegree(degree)
                    .setStartTime(startTime)
                    .setDeadline(deadLine)
                    .setValid(valid)
                    .setId(id);
            return postService.select(post, current, pageSize);
        } catch (RuntimeException e) {
            return getFailPageResult(404, "查询岗位失败" + e.getMessage());
        }
    }

    @ApiOperation(value = "查询岗位（主键）", notes = "查询岗位（主键）")
    @GetMapping(value = "/selectById")
    @ResponseBody
    public Response<Post> selectById(@RequestParam Long id) {
        try {
            return getSuccessResult(postMapper.selectByPrimaryKey(id));
        } catch (RuntimeException e) {
            return getFailResult(404, "查询岗位失败" + e.getMessage());
        }
    }

    @ApiOperation(value = "懒加载查询岗位", notes = "懒加载查询岗位")
    @PostMapping(value = "/selectLazy")
    @ResponseBody
    public PageLazyResult<List<Post>> select(@RequestParam Integer count,
                                                          @RequestParam(required = false) String name, @RequestParam(required = false) String college,
                                                          @RequestParam(required = false) String description, @RequestParam(required = false) String require,
                                                          @RequestParam(required = false) String education, @RequestParam(required = false) String degree,
                                                          @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime deadLine) {
        try {
            Post post = new Post();
            post.setName(name)
                    .setCollege(college)
                    .setDescription(description)
                    .setRequire(require)
                    .setEducation(education)
                    .setDegree(degree)
                    .setStartTime(startTime)
                    .setDeadline(deadLine);
            return postService.selectLazy(post, count);
        } catch (RuntimeException e) {
            return getFailPageLazyResult(404, "查询岗位失败" + e.getMessage());
        }
    }
}
